package com.siyuan.blog.web.system;

import com.baomidou.mybatisplus.plugins.Page;
import com.siyuan.blog.domain.system.Role;
import com.siyuan.blog.common.vo.JSONResponse;
import com.siyuan.blog.common.vo.LayTableResponse;
import com.siyuan.blog.common.vo.PermissionTreeNode;
import com.siyuan.blog.domain.system.Resources;
import com.siyuan.blog.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 角色管理添加页
     * @return
     */
    @RequestMapping("/ui/add")
    public String addUI(){
        return "system/role/add";
    }

    /**
     * 角色管理添加页
     * @return
     */
    @RequestMapping("/ui/edit")
    public String editUI(Integer id, ModelMap modelMap){
        Role role = roleService.selectById(id);
        modelMap.put("role",role);
        return "system/role/edit";
    }

    /**
     * 角色管理列表页
     * @param resources
     * @return
     */
    @RequestMapping("/ui/list")
    public String listUI(Resources resources){
        return "system/role/list";
    }



    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Role> resourcesPage = roleService.selectPage(new Page<Role>(page, limit));
        return new LayTableResponse(resourcesPage.getRecords(),resourcesPage.getTotal());
    }

    /**
     *
     * @param role
     * @param resourcesCodes   //当前角色所拥有的资源Code ，如  1,2,3,4,5,6
     * @return
     */
    @RequestMapping("/save")
    public String save(Role role,String resourcesCodes){
        String[] resCodes = new String[0];
        if(null != resourcesCodes){
            resCodes = resourcesCodes.split(",");
        }
        roleService.insert(role,resCodes);
        return "redirect:/role/ui/list";
    }

    /**
     *
     * @param role
     * @param resourcesCodes   //当前角色所拥有的资源Code ，如  1,2,3,4,5,6
     * @return
     */
    @RequestMapping("/update")
    public String update(Role role,String resourcesCodes){
        String[] resCodes = new String[0];
        if(null != resourcesCodes){
            resCodes = resourcesCodes.split(",");
        }
        roleService.update(role,resCodes);
        return "redirect:/role/ui/list";
    }


    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Integer> ids){
        roleService.deleteBatchIds(ids);
        return "redirect:/role/ui/list";
    }

    /**
     * 获取权限树数据
     * @param roleId
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public JSONResponse permissionTree(Integer roleId){
        List<PermissionTreeNode> treeNodes = roleService.permissionTree(roleId);
        return JSONResponse.success("加载成功",treeNodes);
    }
}
